package com.upb.webserver.user;

import com.upb.webserver.common.exception.WebServerException;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.dao.entity.TbUser;
import com.upb.webserver.dao.mapper.TbUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class UserManager {
    @Autowired
    private TbUserMapper userMapper;

    /**
     * @param userId
     * @return
     */
    public TbUser verifyUserId(int userId) {
        log.info("start exec method [verifyUserId]. userId:{}", userId);
        TbUser tbUser = userMapper.selectByPrimaryKey(userId);
        if (null == tbUser) {
            log.warn("fail exec method [verifyUserId]. not found record by userId{}", userId);
            throw new WebServerException(ConstantCode.INVALID_USER_ID);
        }
        log.info("success exec method [verifyUserId]. result:{}", JsonUtils.objToString(tbUser));
        return tbUser;
    }

}
